package com.threaddemo2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Goods {
    public volatile static int count;
    static Object lock = new Object();
    public int store;

    static Lock reentrantLock = new ReentrantLock();

    /**
     * 静态方法 拿到的是class对象锁
     */
    public static void add(){
        synchronized (lock){
            Goods.count++;
            System.out.println(Goods.count);
        }
    }

    public  static void add2(){
        Goods.count++;
        System.out.println(Goods.count);
    }

    public static void add3(){
        try{
            reentrantLock.lock();
            Goods.count++;
            System.out.println(Goods.count);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

}
